layui.define(['jquery', 'element', 'table'], function (exports) {
    "use strict";

    /**
     * 常用封装类
     * */
    var MOD_NAME = 'common',
        $ = layui.jquery,
        table = layui.table,
        element = layui.element;

    var common = new function () {

        /**
         * 获取当前表格选中字段
         * @param obj 表格回调参数
         * @param field 要获取的字段
         * */
        this.checkField = function (obj, field) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                return "";
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i][field] + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            return ids;
        }

        /**
         * 当前是否为与移动端
         * */
        this.isModile = function () {
            if ($(window).width() <= 768) {
                return true;
            }
            return false;
        }

        /**
         * 打开大弹框
         * @returns {string[]}
         */
        this.screenBig = function () {
            let width, height;
            if (!this.isModile()) {
                if (typeof width !== 'number' || width === 0) {
                    width = $(window).width() * 0.8;
                }
                if (typeof height !== 'number' || height === 0) {
                    height = $(window).height() - 20;
                }
                return [width + 'px', height + 'px'];
            } else {
                return ['100%', '100%'];
            }
        }

        /**
         * 打开中弹框
         * @returns {string[]}
         */
        this.screenMid = function () {
            let width, height;
            if (!this.isModile()) {
                if (typeof width !== 'number' || width === 0) {
                    width = $(window).width() * 0.5;
                }
                if (typeof height !== 'number' || height === 0) {
                    height = $(window).height() * 0.6;
                }
                return [width + 'px', height + 'px'];
            } else {
                return ['100%', '100%'];
            }
        }

        /**
         * 打开中弹框
         * @returns {string[]}
         */
        this.screenLongMid = function () {
            let width, height;
            if (!this.isModile()) {
                if (typeof width !== 'number' || width === 0) {
                    width = $(window).width() * 0.5;
                }
                if (typeof height !== 'number' || height === 0) {
                    height = $(window).height() * 0.9;
                }
                return [width + 'px', height + 'px'];
            } else {
                return ['100%', '100%'];
            }
        }

        /**
         * 打开小弹框
         * @returns {string[]}
         */
        this.screenSmall = function () {
            if (!this.isModile()) {
                return ['500px', '450px'];
            } else {
                return ['100%', '100%'];
            }
        }

        /**
         * 刷新表单__token__
         * 直接赋值到表单
         */
        this.getToken = function (root) {
            $.get(root + "/Index/getToken", function (data) {
                document.getElementById("__token__").value = data;
            });
        }


        /**
         * 提交 json 数据
         * @param data 提交数据
         * @param href 提交接口
         * @param table 刷新父级表
         *
         * */
        this.submit = function (data, href, table, callback) {
            $.ajax({
                url: href,
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: callback != null ? callback(result) : function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload(table);
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
        }
    }
    exports(MOD_NAME, common);
});
